"""操作数据库"""
import pymysql
import re


def useMyseql(sql: str, host='47.108.206.100', user='student', password='stu2022', db='mall', port=3306):
    # 创建一个数据库的连接对象
    db = pymysql.connect(host=host, user=user, password=password, db=db, port=port)
    # 创建游标
    cursor = db.cursor()
    # 定义语句的类型,查询语句需要返回查询结果,而Insert,update,delete需要commit才会改变数据库
    pattern = '^select'
    result = re.match(pattern, sql.lower())
    if result is not None:
        cursor.execute(sql)
        return cursor.fetchall()
    else:
        cursor.execute(sql)
        db.commit()

    cursor.close()
    db.cursor()
